<?Php
	require_once('../Model/getconfig.php');
	//require_once('includes/checksession.php');
	$randomnumer= rand(5, 100000);
	
	function getExtension($str) {
		$i = strrpos($str,".");
		if (!$i) { return ""; }
			$l = strlen($str) - $i;
			$ext = substr($str,$i+1,$l);
			return $ext;
	}			

?>
<?php
//If the form is submitted

			$username="F".$randomnumer;//trim($_REQUEST['textUsername']);
			$password=$randomnumer;//trim($_REQUEST['textpassword']);	
			//$confirmpassword=trim($_REQUEST['textconfirmpassword']);		

//contact  details reading			
			//$RegStatus=$_REQUEST['cmbRegStatus'];
			$Name=trim($_REQUEST['texName']);
			$Typeofbusiness=trim($_REQUEST['cmbTypeofbusiness']);	
			$Businesssublist=trim($_REQUEST['cmbBusinesssublist']);	
			$Titile=trim($_REQUEST['texTitile']);
			$Address1=trim($_REQUEST['texaddr']);
			$contState=trim($_REQUEST['cmbcontState']);	
			$District=trim($_REQUEST['cmbDist']);
			$Email=trim($_REQUEST['textEmail']);
			$website=trim($_REQUEST['textwebsite']);				
			$Phone=trim($_REQUEST['phone']);
			$Details=trim($_REQUEST['texdetails']);
			//$coupon=trim($_REQUEST['textregpin']);
			$image1=$_FILES['image1']['name'];
			
		
			if($username === '') {
				$hasError = true;
				$errorMessage = "Please enter a valid Username!";
			}
			else
			{
				$pnid=0;
				$arrMenu=Model_membermaster::checkUserexist($username);
				foreach($arrMenu as $objMenu){ 
					$pnid=$objMenu->getuserid();
					if($pnid>0)
					{							
						$hasError = true;				
						$errorMessage = "You are entered username already exist( Enter new username)!";
					}
				}
							
			}
		
			if($password === '') {
				$hasError = true;
				$errorMessage = "Please enter a valid Password!";
			}

			
		//	if($confirmpassword === '') {
		//		$hasError = true;
		//		$errorMessage = "Please enter a valid Password!";
		//	}
		//	if($password !== $confirmpassword ) {
		//		$hasError = true;
		//		$errorMessage = "Please enter a valid Password ( New password and confirm password are not matching!";
		//	}	
			
			if($Name === '') {
				$hasError = true;
				$errorMessage = "Please enter a valid Name!";
			}
	
			if($Typeofbusiness === '') {
				$hasError = true;
				$errorMessage = "Please select type of business!";
			}
						
			if($Address1 === '') {
				$hasError = true;
				$errorMessage = "Please enter a valid Address!";
			}

			
		/*	if($pincode === '') {
				$hasError = true;
				$errorMessage = "Please enter a valid Pincode!";
			}
			else if (!preg_match("/^[0-9]/", trim($_POST['texpincode']))) {
				$hasError = true;
				$errorMessage = "Please enter a valid Pincode (Digits only)!";
			}				
*/
			
			if($contState === '') {
				$hasError = true;
				$errorMessage = "Please enter a valid state!";
			}
			
			if($District === '') {
				$hasError = true;
				$errorMessage = "Please enter a valid District!";
			}			

	/*		
			if($city === '') {
				$hasError = true;
				$errorMessage = "Please enter a valid city!";
			}
*/
			
/*			if(trim($_POST['textEmail']) === '')  {
			//	$hasError = true;
			} else if (!preg_match("/^[a-z0-9]+([_\\.-][a-z0-9]+)*@([a-z0-9]+([\.-][a-z0-9]+)*)+\\.[a-z]{2,}$/i", trim($_POST['textEmail']))) {
				$hasError = true;
				$errorMessage = "Please enter a valid email address!";
			}	
			
//coupon checking

		$couponletter = substr($coupon, 0, -1);
		if($couponletter=="R")
		{

			$arrUsers=Model_prepaid::checkCoupon($coupon);  
			if(count($arrUsers)>0){
				$objMemmaster=$arrUsers[0];		
				$Couponstatus=$objMemmaster->getCouponstatus();
				
				if($Couponstatus=="Y")
				{			
					$hasError = true;				
					$errorMessage = "Invalid Registration Pin( This Registration Pin is already used)!";
				}						
			}
			else{			
					$hasError = true;				
					$errorMessage = "Invalid Registration Pin( This Registration Pin is not existing)!";
				}
				
		}else{			
					$hasError = true;				
					$errorMessage = "Invalid Registration Pin( This Pin is not allowed for Registration, This pin for Activation)!";
				}												
			
*/
//PHOTO 1 UPLOADING
if ($image1!="") 
{		  

		$randam= rand(5, 100000);		

	//define a maxim size for the uploaded images in Kb
 	 define ("MAX_SIZE","1000"); 

     $errors=0;
 	 $image1=$_FILES['image1']['name'];
 	 if ($image1!="") 
 	 {
 	 $filename = stripslashes($_FILES['image1']['name']);
  	 $extension = getExtension($filename);
 	 $extension = strtolower($extension);
 	 if (($extension != "jpg") && ($extension != "jpeg") && ($extension != "png") && ($extension != "gif")) 
 	 {
 		echo "<div align='center'><span class='style41'>Image  extension type is not allowed for property image</span></div><br>";
		echo "<div align='center'> <a href='javascript:history.back();'>Back</a></div>";
 		$errors=1;
 	 }
 	 else
 	 {
		 $size=filesize($_FILES['image1']['tmp_name']);
		 if ($size > MAX_SIZE*1024)
		 {
			echo "<div align='center'><span class='style41'>Image 1 have exceeded the size limit!</span></div><br>";
			echo "<div align='center'> <a href='javascript:history.back();'>Back</a></div>";
			$errors=1;
		 }
	//	  if($errors!=1) 
	//	 {
		// $image_name="$userid"."$username".".".$extension;
	//	 $image_name="$imgname".".".$extension;
	//	 $newname="../WebUploads/uploadedimages/".$image_name;
	//	 $copied = copy($_FILES['image1']['tmp_name'], $newname);
	//	 }
		// echo $copied;
	// if (!$copied) 
	// {
	//echo "<div align='center'><span class='style41'>Some errors occured while uploading logo! please try later!</span></div><br>";
	//echo "<div align='center'> <a href='javascript:history.back();'>Back</a></div>";
	//	$errors=1;
	// }
	 }
	// if($errors!=1) 
	// {
	 		//$sqlpropimage="update propertyimage set cimagename='$image_name' where nid='$userid' and nslno='$slno' ";
	//		$sqlpropimage="insert into  generalimage (nid,cimagename,c_type) values ('$maxtabvalue','$image_name','GENERAL') "; 
	//		$objDbManager=new Dbo_DbManager();
	//		$intMemmasterId=$objDbManager->dbexecute($sqlpropimage);			
	//	    echo $sqlpropimage;
		//echo "<div align='center'><span class='style41'>Logo Uploaded Successfully!</span></div><br>";
		//	Jscript_phpjs::redirect('home.php?VL=editimagelist&propid='.$propid);
	// }
	 }
	 else {
	 echo "<div align='center'><span class='style41'>Please select an image for uploading</span></div><br>";
	 echo "<div align='center'> <a href='javascript:history.back();'>Back</a></div>";
	 }
}
else
{
	$errors=1;
}																							
		//If there is no error, send the email
		if(!isset($hasError)) {	
		
		$arrMenu=Model_maxtab::get_Maxtabvalue('REGISTRATION');
			foreach($arrMenu as $objMenu){ 
				$maxtabvalue=$objMenu->getValue(); 
				$maxtabvalue=$maxtabvalue+1;	
		}	
			
//echo	$custsql="insert into membermaster (pn_id,c_username,c_password,c_first_name,c_typ_Business,c_business_subfield,c_titile,c_district,c_state,c_address,c_email,c_mobil,c_details,ref_id,c_status) values ('$maxtabvalue','$username','$password','$Name','$Typeofbusiness','$Businesssublist','$Titile','$District','$contState','$Address1','$Email','$Phone','$Details','$exeusername','Y')";		

	$custsql="insert into membermaster (pn_id,c_username,c_password,c_first_name,c_typ_Business,c_business_subfield,c_district,c_state,c_address,c_email,c_website,c_mobil,c_details,ref_id,c_status) values ('$maxtabvalue','$username','$password','$Name','$Typeofbusiness','$Businesssublist','$District','$contState','$Address1','$Email','$website','$Phone','$Details','$exeusername','Y')";	
		//	echo $custsql;		
			$objDbManager=new Dbo_DbManager();
			$intMemmasterId=$objDbManager->dbexecute($custsql);		
			
			$maxtabsql="update maxtab set  nvalue='$maxtabvalue' where ctype='REGISTRATION' ";		
			//echo $custsql;		
			$objDbManager=new Dbo_DbManager();
			$intMemmasterId=$objDbManager->dbexecute($maxtabsql);					
			
			$arrMenu=Model_typeofbusiness::get_Memberscount($Typeofbusiness);
			foreach($arrMenu as $objMenu){ 
				$typeofbusinesscount=$objMenu->getMemberscount(); 
				$typeofbusinesscount=$typeofbusinesscount+1;	
			}				
			
			$arrMenu=Model_businesslist::get_Memberscount($District);
			foreach($arrMenu as $objMenu){ 
				$businesslistcount=$objMenu->getMemberscount(); 
				$businesslistcount=$businesslistcount+1;	
			}	
			
//selecting district count
			$arrMenu=Model_listeddistrict::get_Districtwisecount($District);
			foreach($arrMenu as $objMenu){ 
			$districtcount=$objMenu->getLlisted(); 
			$districtcount=$districtcount+1;	
			}				
			
			$typeofbusinesssql="update typeofbusiness set  N_MEMBERSCOUNT='$typeofbusinesscount' where C_TYPEOFBUSINESS='$Typeofbusiness' ";		
			//echo $typeofbusinesssql;		
			$objDbManager=new Dbo_DbManager();
			$intMemmasterId=$objDbManager->dbexecute($typeofbusinesssql);	
			
			$businesslistsql="update businesslist set  N_MEMBERSCOUNT='$businesslistcount' where C_BUSINESSINFO='$Businesssublist' ";		
			//echo $businesslistsql;		
			$objDbManager=new Dbo_DbManager();
			$intMemmasterId=$objDbManager->dbexecute($businesslistsql);							
			
			$listeddistrictsql="update listeddistrict set  n_listed='$districtcount' where c_district='$District' ";		
			//echo $listeddistrictsql;		
			$objDbManager=new Dbo_DbManager();
			$intMemmasterId=$objDbManager->dbexecute($listeddistrictsql);		
			
			
//photo uploading
	 if($errors!=1) 
	 {
    	$imgname=$maxtabvalue."-".$randam;
		
		// $image_name="$userid"."$username".".".$extension;
		 $image_name="$imgname".".".$extension;
		 $newname="../WebUploads/uploadedimages/".$image_name;
		 $copied = copy($_FILES['image1']['tmp_name'], $newname);
	 }
	 if($errors!=1) 
	 {
	 		//$sqlpropimage="update propertyimage set cimagename='$image_name' where nid='$userid' and nslno='$slno' ";
			$sqlpropimage="insert into  generalimage (nid,cimagename,c_type) values ('$maxtabvalue','$image_name','GENERAL') "; 
			$objDbManager=new Dbo_DbManager();
			$intMemmasterId=$objDbManager->dbexecute($sqlpropimage);			
			 //   echo $sqlpropimage;
			//echo "<div align='center'><span class='style41'>Logo Uploaded Successfully!</span></div><br>";
			//	Jscript_phpjs::redirect('home.php?VL=editimagelist&propid='.$propid);
	 }		 																	
							
			$success=true;
		}
			
?>		
<?php if(isset($success) == true) { ?>
	<div class="ok_box">
		<h3>Thanks, <?php echo $Name;?></h3>
		<p>Your Registration was successfully completed. Please login and upload your details.</p>
	</div>
<?php } ?>

<?php if(isset($hasError) ) { ?>
	<div class="error_box">
		There was an error submitting the form.
		<br />
		<?php echo $errorMessage;?>
	</div>
<?php } ?>